package servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import session.SessionVariables;
import facade.BD;

/**
 * Servlet implementation class EliminarArticuloCarrito
 */
@WebServlet("/EliminarArticuloCarrito")
public class EliminarArticuloCarrito extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EliminarArticuloCarrito() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		BD bd = BD.getInstance();
		
		String codigo = request.getParameter("codigo");
		int codigo_int = -1;
		
		String jspPage = "/VistaCarrito.jsp";
		
		try{
			codigo_int = Integer.parseInt(codigo);
		}catch(Exception e){
			codigo_int = -1;
		}
		
		bd.sacarArticuloCarrito(codigo_int);
		
		SessionVariables.getInstance().setCarrito(bd.getCarrito());
		
		dispatch(jspPage, request, response);
		
	}

    protected void dispatch(String jsp, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	if (jsp != null)
    	{
    	    RequestDispatcher rd = request.getRequestDispatcher(jsp);
    	    rd.forward(request, response);
    	}

	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
